package com.ysd.lis.service.lab.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.yulichang.wrapper.MPJLambdaWrapper;
import com.ysd.common.Result;
import com.ysd.lis.controller.lab.LabMainInfoLogController;
import com.ysd.lis.entity.lab.LabMainInfoLog;
import com.ysd.lis.entity.lab.LabResultAmendment;
import com.ysd.lis.mapper.lab.LabMainInfoLogMapper;
import com.ysd.lis.mapper.lab.LabResultAmendmentMapper;
import com.ysd.lis.service.lab.LabMainInfoLogService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * @author LuoXS
 * @date 2024/5/23
 * version 1.0
 */
@Service
public class LabMainInfoLogServiceImpl extends ServiceImpl<LabMainInfoLogMapper, LabMainInfoLog> implements LabMainInfoLogService {

    @Resource
    LabMainInfoLogMapper mapper;
    @Resource
    LabResultAmendmentMapper resultAmendmentMapper;

    @Override
    public Result pageList(LabMainInfoLogController.PageParam pageParam) {
        Result result;
        try {
            LambdaQueryWrapper<LabMainInfoLog> lambdaQueryWrapper = new LambdaQueryWrapper<>();
            Page<LabMainInfoLog> page = new Page<>();
            page.setCurrent(pageParam.getPageIndex());
            page.setSize(pageParam.getPageSize());

            lambdaQueryWrapper.eq(LabMainInfoLog::getRptGroup, pageParam.getRptGroup());
            lambdaQueryWrapper.like(LabMainInfoLog::getSampleDate, pageParam.getSampleDate());
            lambdaQueryWrapper.eq(LabMainInfoLog::getSampleNumber, pageParam.getSampleNo());
            lambdaQueryWrapper.isNotNull(LabMainInfoLog::getUpdateField);
//            lambdaQueryWrapper.eq(LabMainInfoLog::getMainInfoId, pageParam.getMainInfoId());

            lambdaQueryWrapper.eq(LabMainInfoLog::getDelFlag, 0);
            lambdaQueryWrapper.orderByDesc(LabMainInfoLog::getCreateTime);
            Page<LabMainInfoLog> labInstrSetupPage = mapper.selectPage(page, lambdaQueryWrapper);
            result = Result.succ(labInstrSetupPage);
        } catch (Exception e) {
            e.printStackTrace();
            result = Result.fail("查询失败！");
        }
        return result;
    }

    @Override
    public Result getResultLogPag(LabMainInfoLogController.PageParam pageParam) {
        MPJLambdaWrapper<LabResultAmendment> lambdaQueryWrapper = new MPJLambdaWrapper<>();
        Page<LabResultAmendment> page = new Page<>();
        page.setCurrent(pageParam.getPageIndex());
        page.setSize(pageParam.getPageSize());
        lambdaQueryWrapper.selectAll(LabResultAmendment.class);
        lambdaQueryWrapper.eq(LabResultAmendment::getMaininfoId, pageParam.getMainInfoId());
        lambdaQueryWrapper.eq(LabResultAmendment::getDelFlag, 0);
        lambdaQueryWrapper.orderByDesc(LabResultAmendment::getCreateTime);
        Page<LabResultAmendment> labInstrSetupPage = resultAmendmentMapper.selectJoinPage(page, LabResultAmendment.class, lambdaQueryWrapper);
        return Result.succ(labInstrSetupPage);
    }
}
